import {Component, OnInit} from '@angular/core';
import {PageVoService} from '../../service/page-vo.service';
import {Menu} from '../../entity/menu';
import {PageVo} from '../../entity/page-vo';
import {TpStorageService} from '../../service/tp-storage.service';

@Component({
    selector: 'app-left-nav',
    templateUrl: './left-nav.component.html',
    styleUrls: ['./left-nav.component.css']
})
export class LeftNavComponent implements OnInit {
    menuList: Menu[];

    /**
     * 菜单类型：index=文档索引 {docId}=某个文档的接口菜单 {docId}.{version}=某个文档的历史接口菜单
     */
    menuType: string;

    /**
     * 菜单接口类型
     */
    menuApiType: number;

    hasRest = false;

    hasDubbo = false;

    pageVo: PageVo;

    constructor(
        private pageVoService: PageVoService,
        private storageService: TpStorageService
    ) {
    }

    ngOnInit() {
        this.pageVoService.currentPageVo.subscribe((pageVo: PageVo) => {
            this.pageVo = pageVo;

            const menuApiTypeStr = this.storageService.get('leftMenuApiType:' + this.pageVo.title);
            if (menuApiTypeStr) {
                this.menuApiType = Number(menuApiTypeStr);
            }

            this.menuList = this.resetApiMenus();
            if (this.menuList && this.menuList.length > 0) {
                const menu = this.menuList[0];
                if (menu.index) {
                    this.menuType = 'index';
                } else {
                    this.menuType = menu['artifactId'];
                }
            }

            if (!this.menuApiType) {
                // 如果没有指定接口类型，则根据菜单类型来判断
                if (this.hasRest) {
                    this.setMenuApiType(0);
                } else if (this.hasDubbo) {
                    this.setMenuApiType(1);
                }
            }
        });
    }

    setMenuApiType(type: number) {
        if (type === this.menuApiType) {
            return;
        }
        this.menuApiType = type;
        this.storageService.set('leftMenuApiType:' + this.pageVo.title, String(type));
        this.menuList = this.resetApiMenus();
    }

    private resetApiMenus(): Menu[] {
        if (!this.pageVo.leftMenus) {
            return [];
        }
        const menus = [];
        this.pageVo.leftMenus.forEach(menu => {
            const item = this.resetApiSubMenus(menu);
            if (item) {
                menus.push(item);
            }
        });
        return menus;
    }

    private resetApiSubMenus(menu: Menu): Menu {
        const item = this.copyMenu(menu);
        if (typeof (menu.menuType) !== 'undefined' && menu.menuType !== 2) {
            switch (menu.menuType) {
                case 0:
                    this.hasRest = true;
                    break;
                case 1:
                    this.hasDubbo = true;
                    break;
            }
            if (menu.active && typeof this.menuApiType === 'undefined') {
                switch (menu.menuType) {
                    case 0:
                        this.menuApiType = 0;
                        break;
                    case 1:
                        this.menuApiType = 1;
                        break;
                }
            }
            if (item.menuType !== 100 && item.menuType !== this.menuApiType) {
                return null;
            }

        }

        if (!menu.children) {
            item.children = null;
            return item;
        }
        for (let i = 0; i < menu.children.length; i++) {
            const child = this.resetApiSubMenus(menu.children[i]);
            if (child) {
                item.children.push(child);
            }
        }
        if (item.children.length === 0) {
            return null;
        }
        return item;
    }

    private copyMenu(menu: Menu): Menu {
        if (!menu) {
            return null;
        }
        return <Menu>{
            index: menu.index,
            id: menu.id,
            name: menu.name,
            icon: menu.icon,
            children: [],
            url: menu.url,
            params: menu.params,
            expanding: menu.expanding,
            active: menu.active,
            deprecated: menu.deprecated,
            menuType: menu.menuType,
            owner: menu.owner,
            artifactId: menu.artifactId
        };
    }
}
